/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.sample.network.swing;

import java.util.Map;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import nemos.remote.rsa.message.IServer;
import nemos.remote.util.RemoteUtil;
import nemos.swing.GenerationException;
import nemos.swing.IComponentFactory;
import nemos.swing.util.BackgroundAction;
import nemos.swing.util.ResourceComponentFactory;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;

/**
 * NetworkPanelFactory : The network panel factory.
 * 
 * @author Romain DUBOIS
 */
@Service(IComponentFactory.class)
@Property(name = IComponentFactory.COMPONENT_TYPE, value = "nemos.sample.network.table")
@Component
public class NetworkTableFactory extends ResourceComponentFactory<JPanel> {

    /** The connection panel resource file name */
    private static final String NETWORK_TABLE = "networkTable"; //$NON-NLS-1$

    /** The network model */
    @Reference(name = "model")
    NetworkModel mModel;

    /** The current server */
    @Reference(name = "server", cardinality = ReferenceCardinality.OPTIONAL_UNARY, policy = ReferencePolicy.DYNAMIC)
    IServer mServer;

    /**
     * Constructor.
     */
    public NetworkTableFactory() {
        super(JPanel.class);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.util.AbstractComponentFactory#createComponent(java.util.Map)
     */
    @Override
    public JPanel createComponent(final Map<String, ?> pInitParams) {
        try {
            loadComponents(pInitParams);

            // Collect components
            final JTable lTable = getComponent("table", JTable.class); //$NON-NLS-1$
            final JButton lKickButton = getComponent("kick", JButton.class); //$NON-NLS-1$
            final JPanel lPanel = getComponent("panel", JPanel.class); //$NON-NLS-1$

            // Configure actions
            lTable.setModel(mModel.getConnectedFrameworks());
            lTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

                @Override
                public void valueChanged(final ListSelectionEvent pE) {
                    final NetworkModel lModel = mModel;
                    if (lModel != null && !pE.getValueIsAdjusting()) {
                        lModel.setSelectedConnection(lTable.getSelectedRow());

                        final String lFramework = lModel.getSelectedFramework();
                        final IServer lServer = mServer;
                        lKickButton.setEnabled(lServer != null && lFramework != null
                                && !lFramework.equals(RemoteUtil.getFrameworkUuid()));
                    }
                }
            });
            lKickButton.setAction(new BackgroundAction<Void, Void>(lKickButton) {

                /** Serial version */
                private static final long serialVersionUID = 1L;

                @Override
                protected Void doInBackground() {
                    final NetworkModel lModel = mModel;
                    final IServer lServer = mServer;
                    if (lModel != null && lServer != null) {
                        final String lFramework = lModel.getSelectedFramework();
                        if (lFramework != null && !lFramework.equals(RemoteUtil.getFrameworkUuid())) {
                            mServer.kick(lFramework);
                        }
                    }
                    return null;
                }
            });

            // Enclosing component
            return lPanel;
        } catch (final GenerationException e) {
            return createDefault(e, null);
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.util.ResourceComponentFactory#getResourceName(java.util.Map)
     */
    @Override
    protected String getResourceName(@SuppressWarnings("unused") final Map<String, ?> pInitParams) {
        return NETWORK_TABLE;
    }

    /**
     * Bind the model.
     * 
     * @param pModel
     *            The model to bind
     */
    public void bindModel(final NetworkModel pModel) {
        mModel = pModel;
    }

    /**
     * Unbind the model.
     * 
     * @param pModel
     *            The model to unbind
     */
    public void unbindModel(@SuppressWarnings("unused") final NetworkModel pModel) {
        mModel = null;
    }

    /**
     * Bind the server.
     * 
     * @param pServer
     *            The server to bind
     */
    public void bindServer(final IServer pServer) {
        mServer = pServer;
    }

    /**
     * Unbind the server.
     * 
     * @param pServer
     *            The server to unbind
     */
    public void unbindServer(@SuppressWarnings("unused") final IServer pServer) {
        mServer = null;
    }
}
